package com.tjtt.tdm.system.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.system.bean.OnlineCounter;
import com.tjtt.tdm.system.bean.OnlineUser;


/**
 * 统计在线用户
 */
public class OnlineCounterAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private String sessionId;

	public String doQuery() {
		try {
			Map<String, OnlineUser> userMap = OnlineCounter.getUserMap();
			List<OnlineUser> userList = new ArrayList<OnlineUser>();
			for (Iterator iter = userMap.keySet().iterator(); iter.hasNext(); ) {
				userList.add(userMap.get((String)iter.next()));
			}
			getRequest().setAttribute("userList", userList);
		}
		catch (RuntimeException e) {
			e.printStackTrace();
			return ERROR;
		}
		return "";
	}

	public void queryOnlineUser(){
		try { 
			Map<String, OnlineUser> userMap = OnlineCounter.getUserMap();
			List<OnlineUser> userList = new ArrayList<OnlineUser>();
			for (Iterator iter = userMap.keySet().iterator(); iter.hasNext(); ) {
				userList.add(userMap.get((String)iter.next()));
			}
			Map<String, String> filterMap = getPagerMap();
			String[] viewArray = { "userId", "userName","loginName","deptName", "realDate"};
			// 查询所有的数据
			PageResult result = new PageResult();
			result.setData(userList);
			result.setRowCount(userList.size());
			String returnStr = getPagerJson(result, viewArray);
			setContentType();
			PrintWriter out = getResponse().getWriter();
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public String delete() {
		try {
			OnlineCounter.deleteUser(sessionId);
		}
		catch (RuntimeException e) {
			e.printStackTrace();
			return ERROR;
		}
		return "";
	}
}
